﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMGROUPMENU : Form
    {
        public string sgroupid;
        public string sgroupname;
        public DBSERVER myADO;//= new DBSERVER();

        public FMGROUPMENU()
        {
            InitializeComponent();
        }

        private void uiTreeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                //Event call by mouse or key-press
                SetNodeCheckStatus(e.Node, e.Node.Checked);
            }
        }

        private void SetNodeCheckStatus(TreeNode tn, bool Checked)
        {
            if (tn == null) return;
            // Check children nodes
            foreach (TreeNode tnChild in tn.Nodes)
            {
                tnChild.Checked = Checked;
                SetNodeCheckStatus(tnChild, Checked);
            }
            // Set parent check status
            TreeNode tnParent = tn;
            int nNodeCount = 0;
            while (tnParent.Parent != null)
            {
                tnParent = (TreeNode)(tnParent.Parent);
                nNodeCount = 0;
                foreach (TreeNode tnTemp in tnParent.Nodes)
                    //if (tnTemp.Checked == Checked)
                    if (tnTemp.Checked)
                        nNodeCount++;

                //if (nNodeCount == tnParent.Nodes.Count)
                //  tnParent.Checked = Checked;
                if (nNodeCount > 0)
                    tnParent.Checked = true;
                else
                    tnParent.Checked = false;
            }
        }

        private void doLoadMenu()
        {
            TreeNode tmp1; TreeNode tmp2;

            string sql = "select a.fmenuid,fmenucaption,fParentId,ISNULL(b.role_id,0) role_id" 
              + "  from sys_menu a"
              + "  left join sys_groupmenu b on b.role_id="+ sgroupid + " and b.fmenuid = a.fmenuid"
              + "  where 1 = 1 "
              + " order by fParentId,fSeqNo,fmenuid";
            DataTable myHead = myADO.query(sql);

            if((myHead is null) || (myHead.Rows.Count == 0))
            {
                return;
            }

            //1.第一级菜单
            DataRow[] drHead = myHead.Select("fParentId ='0'");
            for (int i = 0; i < drHead.Length; i++)
            {
                tmp1 = new TreeNode(drHead[i]["fMenuCaption"].ToString());
                tmp1.Tag = drHead[i]["fmenuid"].ToString();
                tmp1.Checked = false;
                if (!drHead[i]["role_id"].ToString().Equals("0"))
                {
                    tmp1.Checked = true;
                }
                uiTreeView1.Nodes.Add(tmp1);

                //2.第二级菜单
                DataRow[] drItem = myHead.Select("fParentId ='" + drHead[i]["fMenuId"].ToString() + "'");
                for (int j = 0; j < drItem.Length; j++)
                {
                    tmp2 = new TreeNode(drItem[j]["fMenuCaption"].ToString());
                    tmp2.Tag = drItem[j]["fmenuid"].ToString();
                    if (!drItem[j]["role_id"].ToString().Equals("0"))
                    {
                        tmp2.Checked = true;
                    }
                    tmp1.Nodes.Add(tmp2);
                }

            }
            if (uiTreeView1.Nodes.Count > 0)
            {
                uiTreeView1.Nodes[0].Expand();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FMGROUPMENU_Load(object sender, EventArgs e)
        {
            pictureBox1.Left = this.Width - 32 - 2;
            pictureBox1.Top = 2;
            pictureBox1.Height = 32;
            pictureBox1.Width = 32;

            uiTitlePanel1.Text = sgroupname;

            doLoadMenu();
        }

        private void uiPanel1_Click(object sender, EventArgs e)
        {
            if (this.ShowAskDialog("确定保存吗?"))
            {
                myADO.SQLList.Clear();
                string sql = "delete from sys_groupmenu where role_id=" + sgroupid; ;
                myADO.SQLList.Add(sql);

                foreach (TreeNode tnChild in uiTreeView1.Nodes)
                {
                    if (tnChild.Checked)
                    {
                        string s = tnChild.Tag.ToString();
                        sql = "insert into sys_groupmenu(role_id,fMenuId)"
                            + " values(" + sgroupid + "," + s + ")";
                        myADO.SQLList.Add(sql);
                        //Console.WriteLine("当前选择:" + s);
                    }

                    foreach (TreeNode tn in tnChild.Nodes)
                    {
                        if (tn.Checked)
                        {
                            string s = tn.Tag.ToString();
                            sql = "insert into sys_groupmenu(role_id,fMenuId)"
                            + " values(" + sgroupid + "," + s + ")";
                            myADO.SQLList.Add(sql);
                            //Console.WriteLine("当前选择:" + s);
                        }
                    }
                }

                bool IsOk;
                string sError = string.Empty;
                myADO.doSave(out IsOk, out sError);
                if (IsOk)
                {
                    DBCache myCache = new DBCache();
                    myCache.doCache();

                    UIMessageTip.ShowOk("保存成功");
                    this.Close();
                }
            }
        }
    }
}
